/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farmcode.api.runtime.regex;

/**
 *
 * @author oiprado
 */
public class Match {

    private MatchContextCollection matchContextCollection;

    public Match() {
        matchContextCollection = new MatchContextCollection();
    }

    public Match(final MatchContextCollection matchContextCollection) {
        this();
        if(matchContextCollection != null)
            this.matchContextCollection = matchContextCollection;
    }

    
    public String findToken(String string, String key) {
        String returnValue = "";
        for (MatchContext context : matchContextCollection) {
            if (string.contains(context.get("prefix"))) {
                if (context.containsKey(key)) {
                    returnValue = context.get(key);
                } else {
                    returnValue = "The token \'" + key + "\' was not found";
                }
                break;
            }
        }
        return returnValue;
    }

    public MatchContext lookupContext(String string) {
        MatchContext matchContext = null;
        for (MatchContext context : matchContextCollection) {
            if (string.toLowerCase().contains(context.get("prefix").toLowerCase())) {
                matchContext = context;
                break;
            }
        }
        return matchContext;
    }

    public String replace(String string) {
        for (MatchContext context : matchContextCollection) {
            if (string.contains(context.get("prefix"))) {
                string = string.replace(context.get("prefix"), context.get("replace") == null ? "" : context.get("replace"));
                break;
            }
        }
        return string;
    }

    /**
     * @return the matchContextCollection
     */
    public MatchContextCollection getMatchContextCollection() {
        return matchContextCollection;
    }

    /**
     * @param matchContextCollection the matchContextCollection to set
     */
    public void setMatchContextCollection(MatchContextCollection matchContextCollection) {
        this.matchContextCollection = matchContextCollection;
    }
}
